// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// y [author] rhyme_lph
// h [email]  rhymelph@gmail.com
// z [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/transaction_entity.dart';
import 'package:finance_app/src/mixin/filter_transaction_sort_mixin.dart';
import 'package:finance_app/src/mixin/refresh_mixin.dart';
import 'package:finance_app/src/mixin/search_data_list_mixin.dart';
import 'package:finance_app/src/mixin/time_sort_mixin.dart';
import 'package:finance_app/src/mixin/transaction_data_mixin.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/utils/permission_utils.dart';

class AllTransactionsProvider extends BaseProvider
    with
        SearchDataListMixin,
        TimeSortMixin,
        FilterTransactionSortMixin,
        TransactionDataMixin,
        RefreshMixin<dynamic, List<TransactionEntity>> {
  int type;
  List<String> titleList = [
    S.current.all_sales_transactions,
    S.current.all_quotations,
    S.current.all_invoices,
    S.current.all_purchases_transactions,
    S.current.all_pos,
    S.current.all_bills,
    S.current.all_expenses,
    S.current.all_transactions,
    S.current.all_bill__expense,
  ];

  List get filterListMap => [
        [
          if (Config.hasPermission(PermissionUtils.quotation)) 'Quotation',
          if (Config.hasPermission(PermissionUtils.invoice)) 'Invoice',
          // 'Money-In',
        ],
        [
          'Open',
          'Closed',
          'Accepted',
          'Rejected',
        ],
        [
          'Unpaid',
          'Overdue',
          'Paid',
        ],
        [
          if (Config.hasPermission(PermissionUtils.expense)) 'Expense',
          if (Config.hasPermission(PermissionUtils.bill)) 'Bill',
          // 'Money-Out',
          if (Config.hasPermission(PermissionUtils.purchaseOrder))
            'Purchase Order',
        ],
        if (Config.hasPermission(PermissionUtils.purchaseOrder))
          [
            'Open PO',
            'Billed PO',
            'Closed PO',
          ],
        if (Config.hasPermission(PermissionUtils.bill))
          [
            'Unpaid Bill',
            'Overdue Bill',
            'Paid Bill',
          ],
        if (Config.hasPermission(PermissionUtils.expense))
          [
            'Unpaid Expense',
            'Overdue Expense',
            'Paid Expense',
          ],
        [
          if (Config.hasPermission(PermissionUtils.quotation)) 'Quotation',
          if (Config.hasPermission(PermissionUtils.purchaseOrder))
            'Purchase Order',
          if (Config.hasPermission(PermissionUtils.invoice)) 'Invoice',
          if (Config.hasPermission(PermissionUtils.bill)) 'Bill',
          if (Config.hasPermission(PermissionUtils.cashBank)) 'Money-In',
          if (Config.hasPermission(PermissionUtils.expense)) 'Expense',
          if (Config.hasPermission(PermissionUtils.cashBank)) 'Money-Out',
          if (Config.hasPermission(PermissionUtils.creditDebitNote)) ...[
            'Credit Note',
            'Debit Note'
          ]
        ],
        [
          if (Config.hasPermission(PermissionUtils.bill)) ...[
            'Unpaid Bill',
            'Overdue Bill',
            'Paid Bill',
          ],
          if (Config.hasPermission(PermissionUtils.expense)) ...[
            'Unpaid Expense',
            'Overdue Expense',
            'Paid Expense',
          ]
        ],
      ];

  List get typeList => [
        [
          if (Config.hasPermission(PermissionUtils.quotation)) 'quotation',
          if (Config.hasPermission(PermissionUtils.invoice)) 'invoice'
        ],
        ['quotation'],
        ['invoice'],
        [
          if (Config.hasPermission(PermissionUtils.purchaseOrder))
            'purchase_order',
          if (Config.hasPermission(PermissionUtils.bill)) 'bill',
          if (Config.hasPermission(PermissionUtils.expense)) 'expense'
        ],
        ['purchase_order'],
        ['bill'],
        ['expense'],
        [
          if (Config.hasPermission(PermissionUtils.quotation)) 'quotation',
          if (Config.hasPermission(PermissionUtils.invoice)) 'invoice',
          if (Config.hasPermission(PermissionUtils.cashBank)) 'cash_in',
          if (Config.hasPermission(PermissionUtils.purchaseOrder))
            'purchase_order',
          if (Config.hasPermission(PermissionUtils.bill)) 'bill',
          if (Config.hasPermission(PermissionUtils.expense)) 'expense',
          if (Config.hasPermission(PermissionUtils.cashBank)) 'cash_out',
          if (Config.hasPermission(PermissionUtils.creditDebitNote)) ...[
            'credit_note',
            'debit_note'
          ]
        ],
        [
          if (Config.hasPermission(PermissionUtils.bill)) 'bill',
          if (Config.hasPermission(PermissionUtils.expense)) 'expense',
        ]
      ];

  List<String> menuList = ['Make Payment'];

  String get title => titleList[type ?? 0];

  List get filterList => filterListMap[type ?? 0];

  int get isOverdue => currentSelectFilterList
              .where((element) => element.startsWith('Overdue'))
              .length >
          0
      ? 1
      : null;

  @override
  Future get dataFuture => null;

  @override
  List<TransactionEntity> get dataList => transactionList;

  @override
  List<String> get types => filterTypeList;

  List<String> get filterTypeList {
    List<String> resultType = List<String>.from(typeList[type ?? 0]);
    Set<String> filterList = {};
    if (currentSelectFilterList.length > 0) {
      for (final item in currentSelectFilterList) {
        if (filterToKey.containsKey(item)) {
          filterList.add(filterToKey[item]);
        }
      }
    }
    if (filterList.length != 0) {
      resultType.clear();
      resultType.addAll(filterList.toList());
    }
    return resultType;
  }

  @override
  List<String> get statusList => statusTypeList;

  List<String> get statusTypeList {
    List<String> statusResult = [];
    if (currentSelectFilterList.length > 0) {
      for (final item in currentSelectFilterList) {
        if (statusToKey.containsKey(item)) {
          statusResult.add(statusToKey[item]);
        }
      }
    }
    if (statusResult.length == 0) {
      return null;
    }
    return statusResult;
  }

  //选择标签
  void onSelectMenu(String value) {
    unFocus();
    switch (value) {
      case 'Make Payment':
        onAddPayment();
        break;
      case 'Receive Payment':
        onRecordPayment();
        break;
    }
  }

  void onAddPayment() {
    Routes.navigateTo(RouterGenProviders.addPaymentPage);
  }

  void onRecordPayment() {
    Routes.navigateTo(RouterGenProviders.recordPaymentPage);
  }

  void initMenuList(int type) {
    if (type == 5) {
      this.menuList = ['Make Payment'];
    } else if (type == 2) {
      menuList = ['Receive Payment'];
    }
  }
}
